/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveWebServicesV12Helpers.Discussion
{
	/// <summary>
	/// Represents a single entry in a (V3) Discussion tool in a Groove workspace
	/// </summary>
	public class DiscussionEntry : 
		GrooveWebServicesV12HelpersBase,
		System.IComparable
	{
		private GrooveWebServicesV12Helpers.Discussion.DiscussionTool m_DiscussionTool;
		
		private GrooveDiscussionWebService.DiscussionEntry m_DiscussionEntry;

		public DiscussionEntry(GrooveWebServicesV12Helpers.Discussion.DiscussionTool i_DiscussionTool, GrooveDiscussionWebService.DiscussionEntry i_DiscussionEntry)
			: base("", i_DiscussionEntry.URI, i_DiscussionTool.IdentityURL, i_DiscussionTool.Context)
		{
			m_DiscussionTool = i_DiscussionTool;
			m_DiscussionEntry = i_DiscussionEntry;
		}

		/// <summary>
		/// The DiscussionTool which contains this DiscussionEntry
		/// </summary>
		public GrooveWebServicesV12Helpers.Discussion.DiscussionTool DiscussionTool
		{
			get
			{
				return m_DiscussionTool;
			}
		}

		/// <summary>
		/// The body text of this DiscussionEntry
		/// </summary>
		public string Body
		{
			get
			{
				return m_DiscussionEntry.Body;
			}
			set
			{
				//  Do nothing if value already assigned
				if (m_DiscussionEntry.Body == value)
					return;

				string OldValue = m_DiscussionEntry.Body;

				try
				{
					//  Update local instance
					m_DiscussionEntry.Body = value;

					//  Update data
					int_Update();
				}
				catch (System.Exception Exception)
				{
					//  Revert local instance
					m_DiscussionEntry.Body = OldValue;

					throw Exception;
				}
			}
		}

		/// <summary>
		/// The DateTime this DiscussionEntry was created
		/// </summary>
		public System.DateTime Created
		{
			get
			{
				return m_DiscussionEntry.Created;
			}
		}

		/// <summary>
		/// IdentityURL of the Groove user who created this DiscussionEntry
		/// </summary>
		public string CreatedBy
		{
			get
			{
				return m_DiscussionEntry.CreatedBy;
			}
		}

		/// <summary>
		/// ID of this DiscussionEntry
		/// </summary>
		public string ID
		{
			get
			{
				return m_DiscussionEntry.ID;
			}
		}

		/// <summary>
		/// The DateTime this DiscussionEntry was last modified
		/// </summary>
		public System.DateTime Modified
		{
			get
			{
				return m_DiscussionEntry.Modified;
			}
		}

		/// <summary>
		/// IdentityURL of the Groove user who last modified this DiscussionEntry.
		/// </summary>
		public string ModifiedBy
		{
			get
			{
				return m_DiscussionEntry.ModifiedBy;
			}
		}

		/// <summary>
		/// ID of this DiscussionEntry's parent entry, if this is a response
		/// </summary>
		public string ParentID
		{
			get
			{
				return m_DiscussionEntry.ParentID;
			}
		}

		/// <summary>
		/// Subject text of this DiscussionEntry
		/// </summary>
		public string Subject
		{
			get
			{
				return m_DiscussionEntry.Subject;
			}
			set
			{
				//  Do nothing if value already assigned
				if (m_DiscussionEntry.Subject == value)
					return;

				string OldValue = m_DiscussionEntry.Subject;

				try
				{
					//  Update local instance
					m_DiscussionEntry.Subject = value;

					//  Update data
					int_Update();
				}
				catch (System.Exception Exception)
				{
					//  Revert local instance
					m_DiscussionEntry.Subject = OldValue;

					throw Exception;
				}
			}
		}

		/// <summary>
		/// Add a new entry as a response to this DiscussionEntry
		/// </summary>
		/// <param name="i_Subject">Subject</param>
		/// <param name="i_Body">Body</param>
		/// <returns>URI of the new response DiscussionEntry</returns>
		public string AddResponse(string i_Subject, string i_Body)
		{
			//  Get initialized reference to web service
			GrooveDiscussionWebService.GrooveDiscussion DiscussionWebService = GrooveWebServicesV12Helpers.Discussion.DiscussionTool.CreateInitializedDiscussionWebService
				(
				m_IdentityURL,
				m_DiscussionTool.Data,
				Context
				);

			//  Set parameters
			GrooveDiscussionWebService.DiscussionEntry DiscussionEntry = new GrooveDiscussionWebService.DiscussionEntry();
			DiscussionEntry.Subject = i_Subject;
			DiscussionEntry.Body = i_Body;
			DiscussionEntry.ParentID = m_DiscussionEntry.ParentID;

			System.DateTime BeginDateTime = System.DateTime.Now;

			//  Ask service to create discussion entry
			string DiscussionEntryURI = DiscussionWebService.Create(DiscussionEntry);

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("DiscussionWebService::Create, " + TimeSpan.TotalMilliseconds + " ms");
			}

			return DiscussionEntryURI;
		}

		/// <summary>
		/// True if this DiscussionEntry is "unread"
		/// </summary>
		public bool Unread
		{
			get
			{
				return m_DiscussionEntry.Unread;
			}
		}

		/// <summary>
		/// Mark this DiscussionEntry as "read"
		/// </summary>
		public void MarkRead()
		{
			//  Get initialized reference to web service
			GrooveDiscussionWebService.GrooveDiscussion DiscussionWebService = GrooveWebServicesV12Helpers.Discussion.DiscussionTool.CreateInitializedDiscussionWebService
				(
				m_IdentityURL,
				m_DiscussionEntry.URI,
				Context
				);

			System.DateTime BeginDateTime = System.DateTime.Now;

			//  Ask service to delete Discussion entry
			DiscussionWebService.UpdateUnreadMark(false);

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("DiscussionWebService::UpdateUnreadMark, " + TimeSpan.TotalMilliseconds + " ms");
			}
		}

		/// <summary>
		/// Mark this DiscussionEntry as "unread"
		/// </summary>
		public void MarkUnread()
		{
			//  Get initialized reference to web service
			GrooveDiscussionWebService.GrooveDiscussion DiscussionWebService = GrooveWebServicesV12Helpers.Discussion.DiscussionTool.CreateInitializedDiscussionWebService
				(
				m_IdentityURL,
				m_DiscussionEntry.URI,
				Context
				);

			System.DateTime BeginDateTime = System.DateTime.Now;

			//  Ask service to delete Discussion entry
			DiscussionWebService.UpdateUnreadMark(true);

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("DiscussionWebService::UpdateUnreadMark, " + TimeSpan.TotalMilliseconds + " ms");
			}
		}

		/// <summary>
		/// Update this DiscussionEntry
		/// </summary>
		/// <param name="i_Subject">New Subject</param>
		/// <param name="i_Body">New Body</param>
		public void Update(string i_Subject, string i_Body)
		{
			string Subject_OldValue = i_Subject;
			string Body_OldValue = i_Body;

			try
			{
				//  Update local instance
				m_DiscussionEntry.Subject = i_Subject;
				m_DiscussionEntry.Body = i_Body;

				//  Update data
				int_Update();
			}
			catch (System.Exception Exception)
			{
				//  Revert local instance
				m_DiscussionEntry.Subject = Subject_OldValue;
				m_DiscussionEntry.Body = Body_OldValue;

				throw Exception;
			}
		}

		private void int_Update()
		{
			//  Get initialized reference to web service
			GrooveDiscussionWebService.GrooveDiscussion DiscussionWebService = GrooveWebServicesV12Helpers.Discussion.DiscussionTool.CreateInitializedDiscussionWebService
				(
				m_IdentityURL,
				m_DiscussionTool.Data,
				Context
				);

			System.DateTime BeginDateTime = System.DateTime.Now;

			//  Ask service to update discussion entry
			DiscussionWebService.UpdateEntry(m_DiscussionEntry);

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("DiscussionWebService::UpdateEntry, " + TimeSpan.TotalMilliseconds + " ms");
			}
		}

		#region GrooveWebServicesV12HelpersBase Implementation

		public override string BindableURL
		{
			get
			{
				if (System.String.IsNullOrEmpty(m_BindableURL))
					m_BindableURL = m_DiscussionTool.BindableURL + "/" + this.ID;

				return m_BindableURL;
			}
		}

		#endregion
	
		#region IComparable Members

		public int CompareTo(object obj)
		{
			GrooveWebServicesV12Helpers.Discussion.DiscussionEntry CompareDiscussionEntry = (GrooveWebServicesV12Helpers.Discussion.DiscussionEntry)obj;
			if (this.Created > CompareDiscussionEntry.Created)
				return 1;
			else if (this.Created < CompareDiscussionEntry.Created)
				return -1;

			return 0;
		}

		#endregion
	}
}
